package com.houdoudou.bean;

import javax.servlet.http.HttpServletRequest;

public class Machine {

    int id=0;
	String secret="";
	String owner="";
	int boxNum=0;
	int boxVolume=0;
	String name="";
	String address="";
	String search="";
	double longitude=0;
	double latitude=0;
	int  inuse=0;  //1 使用，2不使用
	int  online=0; //1 在线，2不在线。
	int  failure=0; //1 正常，2故障。
	String tempCfg="000"; //1表示常温，2加热，3之类
	String timeCfg="000";  //1 启用，2 不启用
	String timeAEM="0000";
	String timeAAM="0000";
	String timeTLM="0000";
	String timeRLM="0000";
	String timeAEN="0000";
	String timeAAN="0000";
	String timeTLN="0000";
	String timeRLN="0000";
	String timeAEE="0000";
	String timeAAE="0000";
	String timeTLE="0000";
	String timeRLE="0000";
	public Machine() {
		
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getSecret() {
		return secret;
	}
	public void setSecret(String secret) {
		this.secret = secret;
	}
	public String getOwner() {
		return owner;
	}
	public void setOwner(String owner) {
		this.owner = owner;
	}
	public int getBoxNum() {
		return boxNum;
	}
	public void setBoxNum(int boxNum) {
		this.boxNum = boxNum;
	}
	public int getBoxVolume() {
		return boxVolume;
	}
	public void setBoxVolume(int boxVolume) {
		this.boxVolume = boxVolume;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getSearch() {
		return search;
	}
	public void setSearch(String search) {
		this.search = search;
	}
	public double getLongitude() {
		return longitude;
	}
	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}
	public double getLatitude() {
		return latitude;
	}
	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}
	public int getInuse() {
		return inuse;
	}
	public void setInuse(int inuse) {
		this.inuse = inuse;
	}
	public int getOnline() {
		return online;
	}
	public void setOnline(int online) {
		this.online = online;
	}
	public int getFailure() {
		return failure;
	}
	public void setFailure(int failure) {
		this.failure = failure;
	}
	public String getTempCfg() {
		return tempCfg;
	}
	public void setTempCfg(String tempCfg) {
		this.tempCfg = tempCfg;
	}
	public String getTimeCfg() {
		return timeCfg;
	}
	public void setTimeCfg(String timeCfg) {
		this.timeCfg = timeCfg;
	}
	public String getTimeAEM() {
		return timeAEM;
	}
	public void setTimeAEM(String timeAEM) {
		this.timeAEM = timeAEM;
	}
	public String getTimeAAM() {
		return timeAAM;
	}
	public void setTimeAAM(String timeAAM) {
		this.timeAAM = timeAAM;
	}
	public String getTimeTLM() {
		return timeTLM;
	}
	public void setTimeTLM(String timeTLM) {
		this.timeTLM = timeTLM;
	}
	public String getTimeRLM() {
		return timeRLM;
	}
	public void setTimeRLM(String timeRLM) {
		this.timeRLM = timeRLM;
	}
	public String getTimeAEN() {
		return timeAEN;
	}
	public void setTimeAEN(String timeAEN) {
		this.timeAEN = timeAEN;
	}
	public String getTimeAAN() {
		return timeAAN;
	}
	public void setTimeAAN(String timeAAN) {
		this.timeAAN = timeAAN;
	}
	public String getTimeTLN() {
		return timeTLN;
	}
	public void setTimeTLN(String timeTLN) {
		this.timeTLN = timeTLN;
	}
	public String getTimeRLN() {
		return timeRLN;
	}
	public void setTimeRLN(String timeRLN) {
		this.timeRLN = timeRLN;
	}
	public String getTimeAEE() {
		return timeAEE;
	}
	public void setTimeAEE(String timeAEE) {
		this.timeAEE = timeAEE;
	}
	public String getTimeAAE() {
		return timeAAE;
	}
	public void setTimeAAE(String timeAAE) {
		this.timeAAE = timeAAE;
	}
	public String getTimeTLE() {
		return timeTLE;
	}
	public void setTimeTLE(String timeTLE) {
		this.timeTLE = timeTLE;
	}
	public String getTimeRLE() {
		return timeRLE;
	}
	public void setTimeRLE(String timeRLE) {
		this.timeRLE = timeRLE;
	}
	
public static Machine beanFromRequest(HttpServletRequest req) throws Exception {

		Machine machine= new Machine();
		
		if(req.getParameter("id")!=null){	
			if(!req.getParameter("id").isEmpty()){
			int id=Integer.parseInt(req.getParameter("id"));
				machine.setId(id);}
	           }
		if(req.getParameter("secret")!=null){		
			if(!req.getParameter("secret").isEmpty()){
			machine.setSecret(req.getParameter("secret"));}
           }
		if(req.getParameter("owner")!=null){	
			if(!req.getParameter("owner").isEmpty()){
			machine.setOwner(req.getParameter("owner"));
           }}
		if(req.getParameter("boxNum")!=null){	
             if(!req.getParameter("boxNum").isEmpty()){
			int boxNum=Integer.parseInt(req.getParameter("boxNum"));
			machine.setBoxNum(boxNum);		}	
	           }
	  	if(req.getParameter("boxVolume")!=null){
	  		if(!req.getParameter("boxVolume").isEmpty()){
				int boxVolume=Integer.parseInt(req.getParameter("boxVolume"));
					machine.setBoxVolume(boxVolume);	}  		
		          }	
	  	if(req.getParameter("name")!=null){	
	  		if(!req.getParameter("name").isEmpty()){	
			machine.setName(req.getParameter("name"));
	  		}
           }
		if(req.getParameter("address")!=null){		
			if(!req.getParameter("address").isEmpty()){
			machine.setAddress(req.getParameter("address"));
           }}
		if(req.getParameter("search")!=null){	
			if(!req.getParameter("search").isEmpty()){
			machine.setSearch(req.getParameter("search"));
           }}
		if(req.getParameter("longitude")!=null){	
			if(!req.getParameter("longitude").isEmpty()){
			double longitude=Double.parseDouble(req.getParameter("longitude"));
			machine.setLongitude(longitude);     }
			  
           }
		if(req.getParameter("longitude")!=null){	
			if(!req.getParameter("longitude").isEmpty()){
			double latitude=Double.parseDouble(req.getParameter("latitude"));
			machine.setLatitude(latitude);}
			
           }
		if(req.getParameter("inuse")!=null){	
			if(!req.getParameter("inuse").isEmpty()){
			int  use=Integer.parseInt(req.getParameter("inuse"));
			machine.setInuse(use);}
			
           }
		if(req.getParameter("online")!=null){	
			if(!req.getParameter("online").isEmpty()){
				int  online=Integer.parseInt(req.getParameter("online"));
				machine.setOnline(online);}
				
           }
		if(req.getParameter("failure")!=null){	
			if(!req.getParameter("failure").isEmpty()){
				int  failure=Integer.parseInt(req.getParameter("failure"));
				machine.setFailure(failure);}
				
           }
		
		if(req.getParameter("tempCfg")!=null){	
			if(!req.getParameter("tempCfg").isEmpty()){
			machine.setTempCfg(req.getParameter("tempCfg"));
           }}
		if(req.getParameter("timeCfg")!=null){	
			if(!req.getParameter("timeCfg").isEmpty()){
			machine.setTimeCfg(req.getParameter("timeCfg"));
           }}
	
		if(req.getParameter("timeAEM")!=null){		
			if(!req.getParameter("timeAEM").isEmpty()){
			machine.setTimeAEM(req.getParameter("timeAEM"));
		}}
		if(req.getParameter("timeAAM")!=null){	
			if(!req.getParameter("timeAAM").isEmpty()){
			machine.setTimeAAM(req.getParameter("timeAAM"));  
		}}
		if(req.getParameter("timeTLM")!=null){	
			if(!req.getParameter("timeTLM").isEmpty()){
			machine.setTimeTLM(req.getParameter("timeTLM"));
           }}
		if(req.getParameter("timeRLM")!=null){	
			if(!req.getParameter("timeRLM").isEmpty()){
			machine.setTimeRLM(req.getParameter("timeRLM"));
           }}
		

		if(req.getParameter("timeAEN")!=null){	
			if(!req.getParameter("timeAEN").isEmpty()){
			machine.setTimeAEN(req.getParameter("timeAEN"));
           }}
		if(req.getParameter("timeAAN")!=null){	
			if(!req.getParameter("timeAAN").isEmpty()){
			machine.setTimeAAN(req.getParameter("timeAAN"));
           }}
		if(req.getParameter("timeTLN")!=null){	
			if(!req.getParameter("timeTLN").isEmpty()){
			machine.setTimeTLN(req.getParameter("timeTLN"));
           }}
		if(req.getParameter("timeRLN")!=null){	
			if(!req.getParameter("timeRLN").isEmpty()){
			machine.setTimeRLN(req.getParameter("timeRLN"));
           }}
		
		
		if(req.getParameter("timeAEE")!=null){	
			if(!req.getParameter("timeAEE").isEmpty()){
			machine.setTimeAEE(req.getParameter("timeAEE"));
           }}
		if(req.getParameter("timeAAE")!=null){	
			if(!req.getParameter("timeAAE").isEmpty()){
			machine.setTimeAAE(req.getParameter("timeAAE"));
           }}
		if(req.getParameter("timeTLE")!=null){	
			if(!req.getParameter("timeTLE").isEmpty()){
			machine.setTimeTLE(req.getParameter("timeTLE"));
           }}
		if(req.getParameter("timeRLE")!=null){	
			if(!req.getParameter("timeRLE").isEmpty()){
			machine.setTimeRLE(req.getParameter("timeRLE"));
           }}
             System.out.println("bean from request:");
			printMachine(machine);
		
		return machine;

		}

    public static void  printMachine(Machine machine){
    	System.out.println("MachineBean  "+"   id="+machine.getId()+" secret="+machine.getSecret()+" owner="+machine.getOwner()
				+"   boxNum="+machine.getBoxNum()+"   boxVolume="+machine.getBoxVolume()+"  name="+machine.getName()
				+"  address="+machine.getAddress()+"  search="+machine.getSearch()+"  longitude="+machine.getLongitude()+"  latitude="+machine.getLatitude()
				+" tempCfg="+machine.getTempCfg()+" timeCfg="+machine.getTimeCfg()
				+"  inuse="+machine.getInuse()+"  online="+machine.getOnline()+"  failure="+machine.getFailure()
				+"  timeAEM="+machine.getTimeAEM()+"   timeAAM="+machine.getTimeAAM()+"  timeTLM="+machine.getTimeTLM()+"   timeRLM="+machine.getTimeRLM()
				+"  timeAEN="+machine.getTimeAEN()+"   timeAAN="+machine.getTimeAAN()+"  timeTLN="+machine.getTimeTLN()+"   timeRLN="+machine.getTimeRLN()
				+"  timeAEE="+machine.getTimeAEE()+"   timeAAE="+machine.getTimeAAE()+"  timeTLE="+machine.getTimeTLE()+"   timeRLE="+machine.getTimeRLE()
				);
    }
   
	public static  Machine merge(Machine machineExist, Machine machineGiven){
		
		
					
		
				if(!machineGiven.getSecret().isEmpty()){
					machineExist.setSecret(machineGiven.getSecret());
				}
			

		
				if(!machineGiven.getOwner().isEmpty()){
					machineExist.setOwner(machineGiven.getOwner());
				}
		
			if(machineGiven.getBoxNum()!=0){	
				
					machineExist.setBoxNum(machineGiven.getBoxNum());
			}
			if(machineGiven.getBoxNum()!=0){	
				
				machineExist.setBoxVolume(machineGiven.getBoxVolume());
		}
		
			if(!machineGiven.getName().isEmpty()){
				machineExist.setName(machineGiven.getName());
			}
			

			
			if(!machineGiven.getAddress().isEmpty()){
				machineExist.setAddress(machineGiven.getAddress());
			}		
	
			if(!machineGiven.getSearch().isEmpty()){
				machineExist.setSearch(machineGiven.getSearch());
			}
	
		if(machineGiven.getLongitude()!=0){	
			
			machineExist.setLongitude(machineGiven.getLongitude());
	    }
		if(machineGiven.getLatitude()!=0){	
			
			machineExist.setLatitude(machineGiven.getLatitude());
	    }
        if(machineGiven.getInuse()!=0){	
			
			machineExist.setInuse(machineGiven.getInuse());
	    }
        if(machineGiven.getOnline()!=0){	
			
			machineExist.setOnline(machineGiven.getOnline());
	    }
        if(machineGiven.getFailure()!=0){	
			
			machineExist.setFailure(machineGiven.getFailure());
	    }

			if(!machineGiven.getTempCfg().equals("000")){
				machineExist.setTempCfg(machineGiven.getTempCfg());
			}

			if(!machineGiven.getTimeCfg().equals("000")){
				machineExist.setTimeCfg(machineGiven.getTimeCfg());
			}	
			if(!machineGiven.getTimeAEM().equals("0000")){
				machineExist.setTimeAEM(machineGiven.getTimeAEM());
			}		
			if(!machineGiven.getTimeAEN().equals("0000")){
				machineExist.setTimeAEN(machineGiven.getTimeAEN());
			}		
			if(!machineGiven.getTimeAEE().equals("0000")){
				machineExist.setTimeAEE(machineGiven.getTimeAEE());
			}

	
			if(!machineGiven.getTimeAAM().equals("0000")){
				machineExist.setTimeAAM(machineGiven.getTimeAAM());
			}
	
			if(!machineGiven.getTimeAAN().equals("0000")){
				machineExist.setTimeAAN(machineGiven.getTimeAAN());
			}
	
			if(!machineGiven.getTimeAAE().equals("0000")){
				machineExist.setTimeAAE(machineGiven.getTimeAAE());
			}
	
			if(!machineGiven.getTimeTLM().equals("0000")){
				machineExist.setTimeTLM(machineGiven.getTimeTLM());
	
			if(!machineGiven.getTimeTLN().equals("0000")){
				machineExist.setTimeTLN(machineGiven.getTimeTLN());
			}
	
			if(!machineGiven.getTimeTLE().equals("0000")){
				machineExist.setTimeTLE(machineGiven.getTimeTLE());
			}
		}		
			if(!machineGiven.getTimeRLM().equals("0000")){
				machineExist.setTimeRLM(machineGiven.getTimeRLM());
			}
	
			if(!machineGiven.getTimeRLN().equals("0000")){
				machineExist.setTimeRLN(machineGiven.getTimeRLN());
			}
			if(!machineGiven.getTimeRLE().equals("0000")){
				machineExist.setTimeRLE(machineGiven.getTimeRLE());
			}
			 System.out.println("bean after merg:");
				printMachine(machineExist);				
		return machineExist;
	}		
	

	public static boolean  checkMachineValid(Machine machine) throws Exception {
		
		if(machine.getSecret().isEmpty()){ throw new BeanParseException("密码为空"); }	
		if(machine.getOwner().length()!=11){ throw new BeanParseException("业主电话位数不对"); }
		if(machine.getBoxNum()==0) {throw new BeanParseException("箱格未设置"); }
		if(machine.getBoxVolume()==0) {throw new BeanParseException("箱格体积未设置"); }
		if(machine.getName().isEmpty()){ throw new BeanParseException("自提柜名称为空"); }
		if(machine.getAddress().isEmpty()){ throw new BeanParseException("自提柜地址为空"); }
		if(machine.getSearch().isEmpty()){ throw new BeanParseException("搜索匹配为空"); }
		if(machine.getLongitude()==0){  throw new BeanParseException("经度未设置");}
		if(machine.getLatitude()==0){  throw new BeanParseException("维度未设置");}
		if(machine.getInuse()==0){  throw new BeanParseException("启用未设置");}
		if(machine.getTempCfg().length()!=3){ throw new BeanParseException("温度配置异常");}
		if(machine.getTimeCfg().length()!=3){ throw new BeanParseException("早中晚配置异常");}
		boolean result=true,morning,noon,evening;	
		morning= machine.getTimeCfg().substring(0, 1).equals("1");
		noon= machine.getTimeCfg().substring(1, 2).equals("1");
		evening= machine.getTimeCfg().substring(2, 3).equals("1");
		if(morning){
		if(	s2i(machine.getTimeAEM()) >=s2i(machine.getTimeAAM())) { throw new BeanParseException("早班最早送达时间晚于约定送达时间");}
		if(	s2i(machine.getTimeAAM()) >=s2i(machine.getTimeTLM())) { throw new BeanParseException("早班约定送达时间晚于最迟取走时间");} 
		if(	s2i(machine.getTimeTLM()) >=s2i(machine.getTimeRLM()))  { throw new BeanParseException("早班最迟取走时间晚于最迟取回时间");}
		}
		if(noon){
			if(	s2i(machine.getTimeAEN()) >=s2i(machine.getTimeAAN())) { throw new BeanParseException("中班最早送达时间晚于约定送达时间");}
			if(	s2i(machine.getTimeAAN()) >=s2i(machine.getTimeTLN())) { throw new BeanParseException("中班约定送达时间晚于最迟取走时间");} 
			if(	s2i(machine.getTimeTLN()) >=s2i(machine.getTimeRLN()))  { throw new BeanParseException("中班最迟取走时间晚于最迟取回时间");}
			}
		if(evening){
			if(	s2i(machine.getTimeAEE()) >=s2i(machine.getTimeAAE())) { throw new BeanParseException("晚班最早送达时间晚于约定送达时间");}
			if(	s2i(machine.getTimeAAE()) >=s2i(machine.getTimeTLE()))  { throw new BeanParseException("晚班约定送达时间晚于最迟取走时间");}
			if(	s2i(machine.getTimeTLE()) >=s2i(machine.getTimeRLE()))  { throw new BeanParseException("晚班最迟取走时间晚于最迟取回时间");}
			}
		if(morning&&noon){
			if(	s2i(machine.getTimeAEN()) <=s2i(machine.getTimeRLM()))  { throw new BeanParseException("中班最早送达时间早于早班最晚取回达时间");}
		}
		if(evening&&noon){
			if(	s2i(machine.getTimeAEE()) <=s2i(machine.getTimeRLN()))  { throw new BeanParseException("晚班最早送达时间早于中班最晚取回达时间");}
		}
		if(evening&&morning){
			if(	s2i(machine.getTimeAEE()) <=s2i(machine.getTimeRLM()))  { throw new BeanParseException("晚班最早送达时间早于早班最晚取回达时间");}
		}
		
		return result;
	}
	
	private static int s2i(String str) throws Exception {
		if(str.length()!=4) { throw new BeanParseException("时间格式，不对");}
		int result= Integer.parseInt(str);		
		if((result/100>23)||(result/100<0)){ throw new BeanParseException("时间值，不对");}
		if((result%100>59)||(result%100<0)){ throw new BeanParseException("时间值，不对");}	
		return 		result; 
	 }
	
	public static  String  getSearchSql(Machine machine){
		String sql="select * from machine";
		int count=0;
		if(machine.getId()!=0){ sql="select * from machine where id = "+machine.getId()+""; return sql;
		 }else{
			
			if(!machine.getOwner().isEmpty()) {	
				if(count==0) {sql= sql+" where owner = "+machine.getOwner();
				}else{
					sql= sql+" and  owner = "+machine.getOwner();
				  }  
				  count++; }
			if(!machine.getSearch().isEmpty()) {	
				if(count==0) {sql= sql+" where search like '%"+machine.getSearch()+"%' ";
				}else{
					sql= sql+" and  search like '%"+machine.getSearch()+"%' ";
				  }  
				  count++; }
			if(machine.getInuse()!=0) {	
				if(count==0) {sql= sql+" where inuse = "+machine.getInuse();
				}else{
					sql= sql+" and  inuse = "+machine.getInuse();
				  }  
				  count++; }
			if(machine.getOnline()!=0) {	
				if(count==0) {sql= sql+" where online = "+machine.getOnline();
				}else{
					sql= sql+" and  online = "+machine.getOnline();
				  }  
				  count++; }
			if(machine.getFailure()!=0) {	
				if(count==0) {sql= sql+" where failure = "+machine.getFailure();
				}else{
					sql= sql+" and  failure = "+machine.getFailure();
				  }  
				  count++; }
		}
	
		return sql;
	}
	public static Machine encrypt(Machine machine) throws Exception{
		
		 String ciphertext = PasswordUtil.encrypt(machine.getSecret());  
	        machine.setSecret(ciphertext);
		return machine;
	}
	public static Machine decrypt(Machine machine) throws Exception{
		
		 String plaintext = PasswordUtil.decrypt(machine.getSecret());  
	        machine.setSecret(plaintext);
		return machine;
	}	
	
	public static Machine transEncrypt(Machine machine) throws Exception{
		
		 String ciphertext = TransPasswordUtil.encrypt(machine.getSecret());  
	        machine.setSecret(ciphertext);
		return machine;
	}
	public static Machine transDecrypt(Machine machine) throws Exception{
		
		 String plaintext = TransPasswordUtil.decrypt(machine.getSecret());  
	        machine.setSecret(plaintext);
		return machine;
	}
	
	
}
